// EPropertyTextblockWindow.cpp: implementation of the EPropertyTextblockWindow class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EPropWnd.h"
#include "EPropertyTextblock.h"
#include "EPropertyTextblockA.h"
#include "EPropListCtrl.h"
#include "EPropertyTextblockWindow.h"

#define BUTTON_PANEL_HEIGHT 30
#define PANEL_BUTTON_HEIGHT 22

#define PTB_ID_EDIT 1
#define PTB_ID_BUTTON_OK 2
#define PTB_ID_BUTTON_CANCEL 3

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(EPropertyTextblockWindow, CFrameWnd)
    //{{AFX_MSG_MAP(EPropWndEdit)
    ON_WM_CLOSE()
    ON_WM_SIZE()
    ON_WM_ERASEBKGND()
    ON_BN_CLICKED(PTB_ID_BUTTON_OK, OnButtonOk)
    ON_BN_CLICKED(PTB_ID_BUTTON_CANCEL, OnClose)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

EPropertyTextblockWindow::EPropertyTextblockWindow(EPropertyTextblock *pPropertyTextblock, EPropWnd *pCtrl)
    : m_pCtrl(pCtrl)
    , m_pPropertyTextblock(pPropertyTextblock)
    , m_pPropertyTextblockA(nullptr)
{}

EPropertyTextblockWindow::EPropertyTextblockWindow(EPropertyTextblockA *pPropertyTextblockA, EPropWnd *pCtrl)
    : m_pCtrl(pCtrl)
    , m_pPropertyTextblock(nullptr)
    , m_pPropertyTextblockA(pPropertyTextblockA)
{}

EPropertyTextblockWindow::~EPropertyTextblockWindow()
{}

BOOL EPropertyTextblockWindow::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext *pContext)
{
    CRect cr;
    GetClientRect(&cr);

    CRect EditRect = cr;
    EditRect.bottom -= BUTTON_PANEL_HEIGHT;

    CRect PanelRect = cr;
    PanelRect.top = EditRect.bottom;

    //
    // edit panel
    //
    m_Edit.Create(WS_TABSTOP | WS_VSCROLL | ES_MULTILINE | WS_CHILD | WS_VISIBLE, EditRect, this, PTB_ID_EDIT);
    m_Edit.SetFont(m_pCtrl->GetFont());

    //
    // button panel
    //
    m_ButtonPanel.Create(_T(""), WS_CHILD | WS_VISIBLE | WS_DLGFRAME, PanelRect, this);
    m_ButtonPanel.GetClientRect(&cr);

    m_ButtonOK.Create(_T("OK"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 10, 10), this, PTB_ID_BUTTON_OK);
    m_ButtonOK.SetFont(m_pCtrl->GetFont());

    m_ButtonCancel.Create(_T("Cancel"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 80, 80), this, PTB_ID_BUTTON_CANCEL);
    m_ButtonCancel.SetFont(m_pCtrl->GetFont());

    return TRUE;
}

//pass on to the property
void EPropertyTextblockWindow::OnButtonOk()
{
    if(m_pPropertyTextblock) {
        m_pPropertyTextblock->OnWindowButtonOk();
    } else if(m_pPropertyTextblockA) {
        m_pPropertyTextblockA->OnWindowButtonOk();
    }
}

//pass on to the property
void EPropertyTextblockWindow::OnClose()
{
    if(m_pPropertyTextblock) {
        m_pPropertyTextblock->OnWindowButtonCancel();
    } else if(m_pPropertyTextblockA) {
        m_pPropertyTextblockA->OnWindowButtonCancel();
    }
}

BOOL EPropertyTextblockWindow::OnEraseBkgnd(CDC *pDC)
{
    CRect cr;
    GetClientRect(&cr);
    pDC->FillSolidRect(0, 0, cr.Width(), cr.Height(), GetSysColor(COLOR_3DFACE));
    return TRUE;
}

void EPropertyTextblockWindow::OnSize(UINT nType, int cx, int cy)
{
    CRect cr(0, 0, cx, cy);

    m_EditRect = cr;
    m_EditRect.bottom -= BUTTON_PANEL_HEIGHT;

    m_PanelRect = cr;
    m_PanelRect.top = m_EditRect.bottom;

    m_Edit.SetWindowPos(NULL, m_EditRect.left, m_EditRect.top, m_EditRect.Width(), m_EditRect.Height(), SWP_NOZORDER);
    m_ButtonPanel.SetWindowPos(NULL, m_PanelRect.left, m_PanelRect.top, m_PanelRect.Width(), m_PanelRect.Height(), SWP_NOZORDER);

    CRect ButtonRect;
    ButtonRect.top = m_PanelRect.top + (m_PanelRect.Height() - PANEL_BUTTON_HEIGHT) / 2;
    ButtonRect.bottom = ButtonRect.top + PANEL_BUTTON_HEIGHT;
    ButtonRect.left = 5;
    ButtonRect.right = ButtonRect.left + 60;
    m_ButtonOK.SetWindowPos(NULL, ButtonRect.left, ButtonRect.top, ButtonRect.Width(), ButtonRect.Height(), SWP_NOZORDER);

    ButtonRect.left = ButtonRect.right + 5;
    ButtonRect.right = ButtonRect.left + 60;
    m_ButtonCancel.SetWindowPos(NULL, ButtonRect.left, ButtonRect.top, ButtonRect.Width(), ButtonRect.Height(), SWP_NOZORDER);
}

std::wstring EPropertyTextblockWindow::GetEditText()
{
    //
    // edit box had \r\n at every \n
    //
    wchar_t c[256];
    ::GetWindowTextW(m_Edit.GetSafeHwnd(), c, 255);
    CStringW s(c);
    s.Remove(L'\r');
    return s.GetString();
}

void EPropertyTextblockWindow::SetEditText(const std::wstring &s)
{
    //
    // edit box needs \r\n at every newline, so every newline must
    // be converted to also contain \r
    //
    // 1) remove \r
    // 2) replace \n with \r\n
    //
    CStringW t = s.c_str();
    std::wstring u;

    //
    // fjern \r
    //
    t.Remove('\r');

    //
    // insert \r at each \n
    //

    for(int i = 0; i < t.GetLength(); i++) {
        wchar_t c = t[i];

        if(c == L'\n') {
            u += L"\r\n";
        } else {
            u += c;
        }
    }

    ::SetWindowTextW(m_Edit.GetSafeHwnd(), u.c_str());
}